
<?php
    include "../database.php";
    $notSet="/";
    if(isset($_GET['story_id'])) {
        $story_id = $_GET['story_id'];
        
        $query = "SELECT project_id, story_title FROM story WHERE story_id = '$story_id'";
        $project_id = mysql_fetch_array(executeQuery($query))[0];
        $story_title = mysql_fetch_array(executeQuery($query))[1];
        
        $query = "SELECT * FROM task WHERE story_id = '$story_id'";
        $res = executeQuery($query);
        if(mysql_num_rows($res)>0){
            $result = "<h3>Story: $story_title</h3><input type='hidden' id='story_id' value='$story_id'/>
                    <table id='tasks_dialog_table' class='tasks_table'>
                     <thead>
                            <td>tasks</td><td>delete</td><td>member</td><td>estimate (h)</td>
                     </thead>";
            while($row = mysql_fetch_array($res)){
                $task_id = $row['task_id'];
                $task_title = $row['task_title'];
                $assigned_id = $row['assigned_id'];
                $task_status = $row['task_status'];
                $task_time = ($row['task_time']<0)?"":$row['task_time'];
                if($assigned_id == 0){
                    $result .= "<tr>
						<td><input name='task_id' type='hidden' value='$task_id' />$task_title</td><td><a class='delete_task'>delete</a></td><td>
							<select class='task_member'>".
                                                        usersOnProject($project_id)
							."</select>
						</td><td><input class='task_estimate' placeholder='$notSet' value='$task_time' type='text' name='time'/></td>
					</tr>";
                }
                //Naloga je že dodeljena, je ne moremo spreminjati
                else{
                    $result .= "<tr>
                                    <td>$task_title</td><td colspan='3' align='center'>Task is already assigned</td>
                              </tr>";
                }
                
            }
            $result.="</table>
                    <input id='tasks_input_description' type='text' name='tasks_input_description' placeholder='new task description'/>
                    <button id='tasks_submit_new' type='submit'>create</button>";
        }
        else {
            $result = "<h3>Story has no tasks</h3>
                <input type='hidden' id='story_id' value='$story_id'/>
                <input id='tasks_input_description' type='text' name='tasks_input_description' placeholder='new task description'/>
                    <button id='tasks_submit_new' type='submit'>create</button>";
        }
        echo $result;
    }
    //Spreminjanje developerja
    else if(isset($_GET['set_dev']) && isset($_GET['task_id'])){
        echo "setDev";
        $task_id = $_GET['task_id'];
        $dev_id = $_GET['set_dev'];
        //Nastavimo task da je določen uporabniku z dev id na assigned (1)
        $query = "UPDATE task SET assigned_id = '$dev_id', task_status = '1' WHERE task_id = '$task_id'";
        executeQueryRowsAffected($query);
    }
    //Brisanje taskov
    else if(isset($_GET['delete'])){
        echo "delete";
        $task_id = $_GET['delete'];
        //Izbrisemo task z idjem
        $query = "DELETE FROM task WHERE task_id = $task_id";
        executeQuery($query);
    }
    //Editiranje time estimate
    else if(isset($_GET['estimate']) && isset($_GET['task_id'])){
        $estimate = $_GET['estimate'];
        $task_id=$_GET['task_id'];
        //Izbrisemo task z idjem
        $query = "UPDATE task SET task_time='$estimate' WHERE task_id = '$task_id'";
        executeQueryRowsAffected($query);
    }
    else if(isset($_GET['task_title']) && isset($_GET['storyid'])){
        $task_title = $_GET['task_title'];
        $story_id = $_GET['storyid'];
        //Izbrisemo task z idjem
        $query = "INSERT INTO task (story_id, task_title) VALUES ('$story_id','$task_title')";
        executeQueryRowsAffected($query);
    }
    else if(isset($_GET['task_title']) && isset($_GET['task_id'])){
        $task_title = $_GET['task_title'];
        $task_id = $_GET['task_id'];
        //Izbrisemo task z idjem
        $query = "UPDATE task SET task_title='$task_title' WHERE task_id = '$task_id'";
        executeQueryRowsAffected($query);
    }
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
    function usersOnProject($project_id){
        $query = "SELECT id, username FROM user WHERE id IN (SELECT user_id FROM project_user WHERE project_id = '$project_id')";
        $options = "";
        $result = executeQuery($query);
        $options .= "<option value='-1'></option>";
        while ($row = mysql_fetch_array($result)){
            $id = $row['id'];
            $uname = $row['username'];
            $options .= "<option value='$id'>$uname</option>";
        }
        return $options;
        
    }
?>
